*************************************************************************************************************
* Table 6: Effects on dealer expectations
* This table reports the effects of treatment on dealer expectations 

******************************************************************************************

use "${SurveyDataDir}/JHePOS_EL1_Dealer_data.dta",clear
tostring fps_uid, replace
drop if fps_uid == "."
merge 1:1 fps_uid using "${SurveyDataDir}/JHePOS_BL_Dealer_data.dta", keepusing(*_y0) nogen

tostring district_code, replace format("%02.0f")
tostring block_code, replace format("%03.0f")
tostring fps_code, replace format("%02.0f")
*tostring new_fps_code, replace format("%02.0f")


*Merge in Strata (need to fix this later so that we can merge on did and bid)
merge m:1 block_code using "${AdminDataDir}/blockLevel_strata.dta", keep(3) keepusing(strata)
drop _merge

qui distinct district_code block_code, j
assert `r(ndistinct)' == 132 

preserve
use "${SurveyDataDir}/Single_FPS_Dealer_Dataset_For_Validation.dta",clear
keep fps_uid c5_profit_ration
replace c5_profit_ration = . if c5_profit_ration == -88
tempfile EL_profit_data
save `EL_profit_data'

restore

merge 1:1 fps_uid using "`EL_profit_data'", nogen

svyset [pw = pweight]


*Number of originally sampled FPS
scalar fps_obs = 396

*********************************************************
*BL Dealers
*********************************************************
preserve
*Keep only originally sampled dealers
keep if fps_new_yesno == 0
*3 categories - Yes,No, Maybe
recode d1_still_running_fps (1 = 3 Yes) (2 = 1 No) (3 = 2 Maybe), gen(d1_still_running_fps1)

drop d1_still_running_fps 

* create binaries for each income bracket 
gen d1_No = (d1_still_running_fps1 == 1) if !mi(d1_still_running_fps1)
gen d1_Maybe = (d1_still_running_fps1 == 2) if !mi(d1_still_running_fps1)
gen d1_Yes = (d1_still_running_fps1 == 3) if !mi(d1_still_running_fps1)


**
*Bribes

*Yes
qui svy: mean d1_Yes if treatment == 0
qui estat sd 
matrix Mean0 = r(mean)
eststo yesBL: xi: reg d1_Yes treatment i.strata [pw = pweight], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd scalar percent_obs 100*e(N)/fps_obs
estadd local method "OLS"
estadd local esample "Restricted"

*Expected bribe to obtain new license
qui svy: mean b5_extra_license_today if treatment == 0
qui estat sd 
matrix Mean0 = r(mean)
eststo new_bribeBL: xi: reg b5_extra_license_today treatment i.strata [pw = pweight], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd scalar percent_obs 100*e(N)/fps_obs
estadd local method "OLS"
estadd local esample "Restricted"

*Expected bribe to renew license
qui svy: mean b5a_extra_license_renew if treatment == 0 
qui estat sd
matrix Mean0 = r(mean)
eststo renew_bribeBL: xi: reg b5a_extra_license_renew treatment i.strata [pw = pweight], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd scalar percent_obs 100*e(N)/fps_obs
estadd local method "OLS"
estadd local esample "Restricted"

*Dealer Self-Reported Profits
qui svy: mean c5_profit_ration if treatment == 0 
qui estat sd
matrix Mean0 = r(mean)
eststo profitBL: xi: reg c5_profit_ration treatment i.strata [pw = pweight], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd scalar percent_obs 100*e(N)/fps_obs
estadd local method "OLS"
estadd local esample "Restricted"

restore

*********************************************************
*All dealers
*********************************************************

recode d1_still_running_fps (1 = 3 Yes) (2 = 1 No) (3 = 2 Maybe), gen(d1_still_running_fps1)

drop d1_still_running_fps 

* create binaries for each income bracket 
gen d1_No = (d1_still_running_fps1 == 1) if !mi(d1_still_running_fps1)
gen d1_Maybe = (d1_still_running_fps1 == 2) if !mi(d1_still_running_fps1)
gen d1_Yes = (d1_still_running_fps1 == 3) if !mi(d1_still_running_fps1)


**
*Bribes


*Yes
qui svy: mean d1_Yes if treatment == 0
qui estat sd 
matrix Mean0 = r(mean)
eststo yes: xi: reg d1_Yes treatment i.strata [pw = pweight], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd local method "OLS"
estadd local esample "Full"

*Expected bribe to obtain new license
qui svy: mean b5_extra_license_today if treatment == 0
qui estat sd 
matrix Mean0 = r(mean)
eststo new_bribe: xi: reg b5_extra_license_today treatment i.strata [pw = pweight], cluster(block_code) 
estadd scalar control_mean = `=Mean0[1,1]'
estadd local method "OLS"
estadd local esample "Full"

*Expected bribe to renew license
qui svy: mean b5a_extra_license_renew if treatment == 0 
qui estat sd
matrix Mean0 = r(mean)
eststo renew_bribe: xi: reg b5a_extra_license_renew treatment i.strata [pw = pweight], cluster(block_code) 

estadd scalar control_mean = `=Mean0[1,1]'
estadd local method "OLS"
estadd local esample "Full"

*Self-Reported Dealer Profit
qui svy: mean c5_profit_ration if treatment == 0 
qui estat sd
matrix Mean0 = r(mean)
eststo profit: xi: reg c5_profit_ration treatment i.strata [pw = pweight], cluster(block_code) 

estadd scalar control_mean = `=Mean0[1,1]'
estadd local method "OLS"
estadd local esample "Full"

*indicate("Strata FE = *strata*") rename(1.treatment treatment)  drop(_cons ) coeflabels(treatment "Treatment")

#delimit ;
						 
esttab yes yesBL new_bribe new_bribeBL renew_bribe renew_bribeBL profit profitBL using "${OutputDir}/Table6.tex", 
	label b(%12.2gc) se(%12.2gc) booktabs replace nocons width(\hsize)
	rename(1.treatment treatment)  drop(_cons _Istrata*) coeflabels(treatment "Treatment")
	stats(r2_a control_mean N percent_obs esample, 
		labels("Adjusted R\textsuperscript{2}" "Control mean" "Observations" "\% of sample" "Sample") fmt(%12.2g %12.2gc %15.0fc 0 ))
	star(* .10 ** .05 *** .01)
	mgroups("\specialCellCenter{Intends to continue \\ running FPS?}" "\specialCellCenter{Expected bribes \\ to obtain license?}" "\specialCellCenter{Expected bribes \\ to renew license?}" "\specialCellCenter{Profit (self-reported)}",
		pattern(1 0 1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(})  
		span erepeat(\cmidrule(lr){@span}))		
	mlabel(none)
	substitute("main                & " ""
						 "Standard errors in parentheses" ""
						 "\sym{*} \(p<.10\), \sym{**} \(p<.05\), \sym{***} \(p<.01\)" " "
						 " &           .         &"
						 "&                    &"
						 "\multicolumn{7}{l}{\footnotesize  }\\" ""
						 "\multicolumn{7}{l}{\footnotesize }\\" "");

	
#delimit cr
eststo clear 
